package com.example.springboot.config.druid;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.czk.aop.DoJointPoint;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * 阿里巴巴数据源的配置
 * 连接池配置信息
 * @Author:ChenZhangKun
 * @Date: 2020/10/3 15:28
 * */
@Configuration
public class DruidConfig {
    @Bean
    public DoJointPoint doJointPoint(){
        return new DoJointPoint();
    }
    @Bean(name = "alibaba")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druidDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setName("阿里巴巴数据源");
        return dataSource;
    }

    /**
     * 配置druid监控
     * 配置一个后腰管理的servlet
     *
     * @return
     */


    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        // 设置属性
        Map<String, String> initParams = new HashMap<>(10);
        initParams.put("loginUsername", "admin");
        initParams.put("loginPassword", "admin");
        // 默认允许所有访问
        initParams.put("allow", "");
        // 拒绝访问的ip
        bean.setInitParameters(initParams);
        return bean;
    }

    /**
     * 设置拦截器
     *
     * @return
     */

    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        Map<String, String> initParams = new HashMap<>(10);
        initParams.put("exclusions", "*.js,*.css,/druid/*");
        filterRegistrationBean.setInitParameters(initParams);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        return filterRegistrationBean;
    }
}
